package com.huike.web.controller.clues;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.domain.vo.TbFollowup;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.clues.service.ITbFollowupService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.uuid.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 线索跟进记录Controller
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/clues/record")
public class TbClueTrackRecordController extends BaseController {
    @Autowired
    private ITbClueService iTbClueService;
    @Autowired
    private ITbFollowupService iTbFollowupService;
    @Autowired
    private ITbClueTrackRecordService iTbClueTrackRecordService;
    /**
     * 查询线索跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("clueId")Long clueId) {
        List<TbClueTrackRecord> trackRecord = iTbClueTrackRecordService.selectById(clueId);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(trackRecord);
        tableDataInfo.setTotal(trackRecord.size());
        return tableDataInfo;
    }


    /**
     * 新增线索跟进记录
     */


    @PreAuthorize("@ss.hasPermi('clues:record:add')")
    @Log(title = "线索跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClueTrackRecordVo tbClueTrackRecord) {
        TbClue tbClue = new TbClue();
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClue.setSubject(tbClueTrackRecord.getSubject());
        tbClue.setLevel(tbClueTrackRecord.getLevel());
        tbClue.setName(tbClueTrackRecord.getName());
        tbClue.setSex(tbClueTrackRecord.getSex());
        tbClue.setWeixin(tbClueTrackRecord.getWeixin());
        tbClue.setQq(tbClueTrackRecord.getQq());
        tbClue.setAge(tbClueTrackRecord.getAge());

        TbClueTrackRecord trackRecord = new TbClueTrackRecord();
        trackRecord.setClueId(tbClueTrackRecord.getClueId());
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        trackRecord.setSubject(tbClueTrackRecord.getSubject());
        trackRecord.setRecord(tbClueTrackRecord.getRecord());
        trackRecord.setLevel(tbClueTrackRecord.getLevel());
        trackRecord.setCreateTime(DateUtils.getNowDate());
        trackRecord.setNextTime(tbClueTrackRecord.getNextTime());

        try {
            iTbClueTrackRecordService.TbClueTrackRecordInsert(trackRecord,tbClue);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

//        BeanUtils.copyProperties(tbClueTrackRecord,tbClue);
//        System.out.println(tbClue);
        return AjaxResult.success();
    }
}
